package com.clps.rulesregulations.service.impl;

import com.clps.rulesregulations.dao.RoleDao;
import com.clps.rulesregulations.dao.RoleResourceDao;
import com.clps.rulesregulations.domain.AccessFile;
import com.clps.rulesregulations.domain.Role;
import com.clps.rulesregulations.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 角色逻辑处理实现类
 * @author 张超
 * @date 2017/7/11
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleResourceDao roleResourceDao;
    @Override
    public List<Role> findRolesByUserId(Integer userId) {
        return roleDao.findRolesByUserId(userId);
    }

    @Override
    public PageInfo<Role> findRolesByPage(Map<String, Object> condition) {
        Integer pageNum = 1;
        try {
            pageNum = (Integer) condition.get("pageNum");
            pageNum = pageNum==0?1:pageNum;
        }catch (Exception e){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,10);
        List<Role> roles = roleDao.findRolesByPage(condition);
        return new PageInfo<Role>(roles);
    }
    @Override
    public PageInfo<AccessFile> findAccessFileLogByPage(Map<String, Object> condition) {
        Integer pageNum = 1;
        try {
            pageNum = (Integer) condition.get("pageNum");
            pageNum = pageNum==0?1:pageNum;
        }catch (Exception e){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,10);
        List<AccessFile> filelogs = roleDao.findAccessFileLogByPage(condition);
        return new PageInfo<AccessFile>(filelogs);
    }


    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public void delRoleByRoleId(Integer roleId) {
        roleResourceDao.delRoleResourceByRoleId(roleId);
        roleDao.delRoleByRoleId(roleId);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void addRole(Role role) {
        roleDao.addRole(role);
    }

    @Override
    public Role findRoleById(Integer roleId) {
        return roleDao.findRoleById(roleId);
    }

    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    @Override
    public void updateRole(Role role) {
        roleDao.updateRole(role);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.finAll();
    }
}
